package net.hn.hnms.biz.plan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.hn.hnms.biz.plan.domain.PlanOutput;
import net.hn.hnms.biz.plan.vo.PlanOutputVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface PlanOutputMapper extends BaseMapper<PlanOutput> {

    /**
     * 获取当前月计划产量
     *
     */
    @Select("select plan_output planoutput from plan_output where output_plan_type = '1' and mine_code=#{mineCode} and del_flag = '0' and to_char(plan_start_date,'YYYYMM') = to_char((SELECT now()::timestamp),'YYYYMM') limit 1 ")
    Map<String,Object> getPlanMonthOutput(@Param("mineCode") String mineCode);

    /**
     * 获取当前年计划产量
     */
    @Select("select \"sum\"(plan_output) planoutput from plan_output where  output_plan_type = '1' and mine_code=#{mineCode} and del_flag = '0' and to_char(plan_start_date,'YYYY') = to_char((SELECT now()::timestamp),'YYYY') ")
    Map<String, Object> getPlanYearOutput(@Param("mineCode") String mineCode);
    /**
     * 获取当月累计产量
     *
     */
    @Select("select \"sum\"(efo.\"output\") from execution_footage_output efo where del_flag = '0' and mine_code=#{mineCode} and to_char(execution_date,'YYYYMM') = to_char((SELECT now()::timestamp),'YYYYMM')")
    Map<String,Object> getSumMonthOutput(@Param("mineCode") String mineCode);

    /**
     * 获取当年累计产量
     *
     */
    @Select("select \"sum\"(efo.\"output\") from execution_footage_output efo where del_flag = '0' and mine_code=#{mineCode}  and to_char(execution_date,'YYYY') = to_char((SELECT now()::timestamp),'YYYY')")
    Map<String,Object> getSumYearOutput(@Param("mineCode") String mineCode);

    /**
     * 获取回采计划进尺
     *
     */
    @Select("<script> select sum(mining_plan_footage) miningplanfootage from plan_mining where mining_plan_type = '1' and del_flag = '0' and status = '0' " +
            " <if test='type!= null and type == 0 '>"+
            " and to_char(plan_start_date,'YYYYMM') = to_char((SELECT now()::timestamp),'YYYYMM') " +
            " </if>"+
            " <if test='type!= null and type == 1 '>"+
            " and to_char(plan_start_date,'YYYY') = to_char((SELECT now()::timestamp),'YYYY') " +
            " </if>"+
            " <if test='mineCode != null and mineCode != \"\" '> "+
            " and mine_code = #{mineCode} " +
            " </if>"+
            "  </script>")
    Map<String, Object> getMiningPlanFootage(@Param("type") Integer type, @Param("mineCode") String mineCode);

    /**
     * 获取掘进计划进尺
     *
     */
    @Select("<script> select sum(drivage_plan_footage) drivageplanfootage from plan_drivage where drivage_plan_type = '1' and del_flag = '0' and status = '0' " +
            " <if test='type!= null and type == 0 '>"+
            " and to_char(plan_start_date,'YYYYMM') = to_char((SELECT now()::timestamp),'YYYYMM') " +
            " </if>"+
            " <if test='type!= null and type == 1 '>"+
            " and to_char(plan_start_date,'YYYY') = to_char((SELECT now()::timestamp),'YYYY') " +
            " </if>"+
            " <if test='mineCode != null and mineCode != \"\" '> "+
            " and mine_code = #{mineCode} " +
            " </if>"+

            " <if test='drivageId != null and drivageId != \"\" '> "+
            " and drivage_face_id = #{drivageId} " +
            " </if>"+

            "  </script>")
    Map<String, Object> getDrivagePlanFootage(@Param("type") Integer type, @Param("mineCode") String mineCode,@Param("drivageId") String drivageId);

    /**
     * 开拓计划进尺
     *
     */
    @Select(" <script> select sum(development_plan_footage) developmentplanfootage from plan_development where  development_plan_type = '1' and del_flag = '0' and status = '0' " +
            " <if test='type!= null and type == 0 '>"+
            " and to_char(plan_start_date,'YYYYMM') = to_char((SELECT now()::timestamp),'YYYYMM') " +
            " </if>"+
            " <if test='type!= null and type == 1 '>"+
            " and to_char(plan_start_date,'YYYY') = to_char((SELECT now()::timestamp),'YYYY') " +
            " </if>"+
            " </script> ")
    Map<String,Object> getDevelopmentPlanFootage(@Param("type")Integer type);

    /**
     * 获取掘进/回采进尺
     *
     */
    @Select(" <script> select \"sum\"(footage) sumfootage from execution_footage_output where  del_flag = '0' and work_face_type = #{workFaceType} " +
            " <if test='type!= null and type == 0 '>"+
            " and to_char(execution_date,'YYYYMM') = to_char((SELECT now()::timestamp),'YYYYMM') " +
            " </if>"+
            " <if test='type!= null and type == 1 '>"+
            " and to_char(execution_date,'YYYY') = to_char((SELECT now()::timestamp),'YYYY') " +
            " </if>"+

            " <if test='type!= null and type == 2 '>"+
            " and to_char(execution_date,'YYYYMMDD') = to_char((now() + '-1 day'),'YYYYMMDD') " +
            " </if>"+

            " <if test='mineCode != null and mineCode != \"\" '> "+
            " and mine_code = #{mineCode} " +
            " </if>"+

            " <if test='drivageId != null and drivageId != \"\" '> "+
            " and work_face_id = #{drivageId} " +
            " </if>"+

            " </script>")
    Map<String, Object> getFootageByType(@Param("workFaceType") String workFaceType, @Param("type") Integer type, @Param("mineCode") String mineCode,@Param("drivageId") String drivageId);

    /**
     * 获取开拓进尺
     *
     */
    @Select(" <script> select \"sum\"(develop) sumdevelop from execution_footage_output where  del_flag = '0' " +
            " <if test='type!= null and type == 0 '>"+
            " and to_char(execution_date,'YYYYMM') = to_char((SELECT now()::timestamp),'YYYYMM') " +
            " </if>"+
            " <if test='type!= null and type == 1 '>"+
            " and to_char(execution_date,'YYYY') = to_char((SELECT now()::timestamp),'YYYY') " +
            " </if>"+
            " </script> ")
    Map<String,Object> getDevelopmentFootage(@Param("type")Integer type);

    /**
     * 获取巷道维修计划/材料消耗计划(万元)
     *
     */
    @Select(" <script> select \"sum\"(plan_repair) planrepair,\"sum\"(plan_material) planmaterial from plan_other where  del_flag = '0' and other_plan_type = '1' " +
            " <if test='type!= null and type == 0 '>"+
            " and to_char(plan_start_date,'YYYYMM') = to_char((SELECT now()::timestamp),'YYYYMM') " +
            " </if>"+
            " <if test='type!= null and type == 1 '>"+
            " and to_char(plan_start_date,'YYYY') = to_char((SELECT now()::timestamp),'YYYY') " +
            " </if>"+
            " <if test='mineCode != null and mineCode != \"\" '> "+
            " and mine_code = #{mineCode} " +
            " </if>"+
            " </script> ")
    Map<String,Object> getPlanRepair(@Param("type")Integer type, @Param("mineCode") String mineCode);

    /**
     * 获取巷道维修(m)
     *
     */
    @Select(" <script> select \"sum\"(repair) repair from execution_other where  del_flag = '0' " +
            " <if test='type!= null and type == 0 '>"+
            " and to_char(execution_date,'YYYYMM') = to_char((SELECT now()::timestamp),'YYYYMM') " +
            " </if>"+
            " <if test='type!= null and type == 1 '>"+
            " and to_char(execution_date,'YYYY') = to_char((SELECT now()::timestamp),'YYYY') " +
            " </if>"+
            " <if test='mineCode != null and mineCode != \"\" '> "+
            " and mine_code = #{mineCode} " +
            " </if>"+
            " </script> ")
    Map<String,Object> getRepair(@Param("type")Integer type, @Param("mineCode") String mineCode);
    /**
     * 材料消耗(万元)
     *
     */
    @Select(" <script> select \"sum\"(cost_material) costmaterial from execution_cost where  del_flag = '0' " +
            " <if test='type!= null and type == 0 '>"+
            " and to_char(execution_date,'YYYYMM') = to_char((SELECT now()::timestamp),'YYYYMM') " +
            " </if>"+
            " <if test='type!= null and type == 1 '>"+
            " and to_char(execution_date,'YYYY') = to_char((SELECT now()::timestamp),'YYYY') " +
            " </if>"+
            " <if test='mineCode != null and mineCode != \"\" '> "+
            " and mine_code = #{mineCode} " +
            " </if>"+
            " </script> ")
    Map<String,Object> getCostMaterial(@Param("type")Integer type, @Param("mineCode") String mineCode);

    /**
     * 月产量曲线
     */
    @Select("select \"sum\"(t1.output) output,t1.monthdate from (select output,to_char(execution_date,'YYYYMM') monthdate from execution_footage_output where del_flag = '0' and mine_code=#{mineCode} and to_char(execution_date,'YYYY') = to_char((SELECT now()::timestamp),'YYYY')) t1 GROUP BY t1.monthdate order by t1.monthdate asc")
    List<Map<String, Object>> getOutputCurve(@Param("mineCode") String mineCode);

    /**
     * 计划月产量曲线
     *
     */
    @Select(" select \"sum\"(t1.plan_output) planoutput,t1.monthdate from (select plan_output,to_char(plan_start_date,'YYYYMM') monthdate from plan_output where del_flag = '0' and mine_code=#{mineCode} and output_plan_type = '1' and to_char(plan_start_date,'YYYY') = to_char((SELECT now()::timestamp),'YYYY')) t1 GROUP BY t1.monthdate order by t1.monthdate asc")
    List<Map<String,Object>> getPlanOutputCurve(@Param("mineCode") String mineCode);

    /**
     * 获取当年全部月份
     *
     */
    @Select(" SELECT to_char( \"month\", 'yyyymm' ) \"month\" FROM generate_series ( date_trunc( 'year', now( ) ) :: DATE, (date_trunc( 'year', now( ) ) + '1y - 1d':: INTERVAL) :: DATE, '1mon' :: INTERVAL ) \"month\"")
    List<Map<String,Object>> getMonth();




    @Select("<script>" +
            " select " +
            " case when count(output_plan_id)>0 then count(output_plan_id)   else null end  as totalPlan," +
            " sum(plan_output) as totalPlanOutput," +
            " min(plan_start_date) as planStartDate," +
            " max(plan_end_date) as planEndDate" +
            " from plan_output where del_flag ='0' and  mine_code = #{mineCode} " +
            "</script>")
    PlanOutputVO selectPlanOutPutTotalByMineCode(@Param("mineCode") String mineCode);

    @Select("<script>" +
            " select " +
            " case when count(output_plan_id)>0 then count(output_plan_id)   else null end  as totalPlan," +
            " sum(plan_output) as totalPlanOutput," +
            " min(plan_start_date) as planStartDate," +
            " max(plan_end_date) as planEndDate" +
            " from plan_output where del_flag ='0' and  company_code = #{mineCode} " +
            "</script>")
    PlanOutputVO selectDimDrivageFaceTotalByCompanyCodeOrName(@Param("mineCode") String mineCode);


}
